package com.hyg.common.utils;

import android.icu.math.BigDecimal;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;

/*
 *****************************************
 *项目名称:renai
 *@Author jzb
 *创建时间 20211103 09:44
 *
 *****************************************
 */public class ByteUtil {

    private static String TAG = "ByteUtil";
    public static String bytes2hex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        String tmp = null;
        for (byte b : bytes) {
            // 将每个字节与0xFF进行与运算，然后转化为10进制，然后借助于Integer再转化为16进制
            tmp = Integer.toHexString(0xFF & b);
            if (tmp.length() == 1) {
                sb.append(" 0x0").append(tmp);
            }else {
                sb.append(" 0x").append(tmp);
            }
        }
        return sb.toString();
    }

    /*public static byte[] setTempture(byte h,byte l){
        byte[] temp = new byte[2];

        return temp;
    }*/

    public static float getTempture(byte[] bytes) {
        int preNum = bytes[2];
        int afterNum = bytes[3];
        float decimal = 0;
        if (afterNum >= 10 && afterNum <= 99){
            decimal = Float.parseFloat(String.valueOf(preNum).concat(".").concat(String.valueOf(afterNum)));
        }else if (afterNum < 10){
            String str = String.valueOf(preNum).concat(".").concat("0" + String.valueOf(afterNum));
            decimal = Float.parseFloat(str);
        }
        return decimal;
    }
    public static int[] getDepth(byte[] bytes) {
        int length = bytes.length/2;
        int[] res = new int[length];
        int intCount = 0;
        for (int i = 0; i < bytes.length; i++) {
            byte l = bytes[i];
            i++;
            byte h = bytes[i];
            int hbyte = 0xFF & h;
            int lowbyte = 0xFF & l;
            hbyte <<= 8;
            hbyte = hbyte | lowbyte;
            res[intCount++]= hbyte;
        }
        return res;
    }

    /**
     *
     * @param src
     * @param coede "utf8" or "gbk"
     * @return
     */
    public static ArrayList<String> getStrings(String src, String coede){
        BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(src.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
        String line;
        ArrayList<String> reslist = new ArrayList<>();
        try {
            while ( (line = br.readLine()) != null ) {
                if (!line.trim().equals("")) {
                    line = " "+line;//每行可以做加工
                    reslist.add(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reslist;
    }

    /**
     * 显示小数点后两位
     * @param src
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    public static String getshowStr(Double src){
        if (src == 0){
            return String.valueOf(src);
        }
        String dstr = String.valueOf(src);
        Log.e(TAG, "getshowStr: "+dstr);
        BigDecimal bigDecimal = new BigDecimal(dstr);
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(bigDecimal);
    }

    public static byte[] splitInt(int num) {
        byte[] ret= new byte[4];
        for(int i = 0;i < 4;++i) {
            ret[i] = (byte)(num>>(i*8));
        }
        return ret;
    }
    public static int combineIntoInt(byte[] bytes) {
        int ret = 0;
        for(int i = 0;i < bytes.length;++i) {
            ret += ((bytes[i] & 0xFF) << i * 8);
        }
        return ret;
    }

}
